﻿using System.Collections.ObjectModel;
using KinonekoSoftware.Extensions.Collections;
using KinonekoSoftware.Extensions.Ioc;

namespace KinonekoSoftware.ArkSpark.Runtimes.Presenters
{
    public sealed class ParagraphPresenter : DocumentElementPresenter<ParagraphElement>, IArkSparkTextModel, IHighlightDataContext
    {
        private ObservableCollection<IHighlightToken> _highlights;
        
        public ParagraphPresenter(ParagraphElement element) : base(element)
        {
            Highlights = new ObservableCollection<IHighlightToken>();
            
            //
            //
            RaiseUpdated(nameof(Content));
        }

        public IList<IHighlightToken> GetHighlightGlossaries() => Highlights;


        public void UpdateHighlightsInformation()
        {
            IsHighlightInformationChanged++;
        }

        public string Content
        {
            get => TargetElement.Content;
            set
            {
                var oldValue = TargetElement.Content;
                var newValue = value;


                if (oldValue != newValue)
                {

                    //
                    //
                    IsModified            = true;
                    TargetElement.Content = newValue;

                    //
                    // 报告已更改
                    ElementUpdatedEventArgs.OnNext(this, Bus);
                    RaiseUpdated();
                }
            }
        }

        public bool IsModified { get; set; }
        public int  Length     => Content?.Length ?? 0;

        public  ObservableCollection<IHighlightToken> Highlights { get; }
        private int                                   _isHighlightInformationChanged;

        /// <summary>
        /// 获取或设置 <see cref="IsHighlightInformationChanged"/> 属性。
        /// </summary>
        public int IsHighlightInformationChanged
        {
            get => _isHighlightInformationChanged;
            set => SetValue(ref _isHighlightInformationChanged, value);
        }
    }
}